package com.dlc.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.distribution.common.constants.DistributionUserStateEnum;
import com.dlc.shop.distribution.common.dto.DistributionUserIncomeDto;
import com.dlc.shop.distribution.common.dto.StatisticsDisUserIncomeDto;
import com.dlc.shop.distribution.common.model.DistributionUser;
import com.dlc.shop.distribution.common.model.DistributionUserIncome;
import com.dlc.shop.distribution.common.service.DistributionUserIncomeService;
import com.dlc.shop.distribution.common.service.DistributionUserService;
import com.dlc.shop.distribution.common.vo.DistributionOrdersVO;
import com.dlc.shop.security.api.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author yami
 */
@Tag(name = "分销员收入接口")
@RestController
@RequestMapping("/p/distribution/income")
@AllArgsConstructor
public class DistributionUserIncomeController {
    private final DistributionUserIncomeService distributionUserIncomeService;
    private final DistributionUserService distributionUserService;

    @GetMapping("/statistics")
    @Operation(summary = "统计分销员今日、本月的收益")
    public ServerResponseEntity<StatisticsDisUserIncomeDto> statisticsDistributionUserIncome() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        String userId = SecurityUtils.getUser().getUserId();
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是本店的分销员
            throw new YamiShopBindException("yami.distribution.user.no.shop");
        }
        return ServerResponseEntity.success(distributionUserIncomeService.statisticsDistributionUserIncome(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取收益列表")
    public ServerResponseEntity<IPage<DistributionUserIncomeDto>> getDistributionUserIncomePage(PageParam<DistributionUserIncome> page) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        String userId = SecurityUtils.getUser().getUserId();
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是本店的分销员
            throw new YamiShopBindException("yami.distribution.user.no.shop");
        }
        IPage<DistributionUserIncomeDto> distributionUserIncomeDto = distributionUserIncomeService.getDistributionUserIncomePage(page, distributionUser.getDistributionUserId());
        return ServerResponseEntity.success(distributionUserIncomeDto);
    }

    @GetMapping("/myPromotionOrder")
    @Operation(summary = "我的推广订单" , description = "通过状态获取我的推广订单(0:待支付 1:待结算 2:已结算 -1:订单失效)")
    @Parameter(name = "state", description = "状态" )
    public ServerResponseEntity<IPage<DistributionOrdersVO>> getMyPromotionOrderByState(PageParam<DistributionOrdersVO> page,
                                                                                        @RequestParam(value = "state", required = false) Integer state) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是本店的分销员
            throw new YamiShopBindException("yami.distribution.user.no.shop");
        }
        if (!Objects.equals(distributionUser.getState(), DistributionUserStateEnum.NORMAL.getValue())) {
            // 您的分销身份异常，请联系管理员
            throw new YamiShopBindException("yami.distribution.user.no.distributor1");
        }
        IPage<DistributionOrdersVO> result = distributionUserIncomeService.getMyPromotionOrderByState(page, distributionUser.getDistributionUserId(), state);
        return ServerResponseEntity.success(result);
    }
}
